package com.greenleaf.util;
import org.springframework.stereotype.Component;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;

@Component
public class PayUtil {
	private final String APP_ID = "2021000118661035";//应用信息 appid
	//应用私钥
	private final String APP_PRIVATE_KEY ="MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQCkS8zymUV8uKMsDsvUH4tedRIhIOt1kZseTLmEKOjfxhpIaC/OnnNZAj1HZR8Dgb+ZMxBgFMlvXHIme7GljBd7CQ6fhkyxPFztE+zcmO2zqQAoI4XFOnD3LXty+3OvcqsTU/SthDG3dbOwMIkMfoCWOBB3hLD4HfPJWIvwjNu7BDpt27voHSXzruBPTK/aLSsL8UEHgSc6FMu1jWF3zijwbv/qlhcKnJ5uYhZD+bfHtnS+TdC+hkN/Kv7VoBGAp1efYvBaAL3dooTL+UThp4k5OnCONNI5Js9orUHKkzBA4aSRGHXQ//7WHQwohNjRQuNoZFTDFfYTmPJpPokdaJsXAgMBAAECggEBAKHrZqANOHNz0LSiHHYBZKyB+o0V0Zi8GMFi4k8CtoebyNllfQBIHuwxDc2oNkmRfQ/ySIg41FFhLcsJiwnZqGaEhZnolPrzv62TPpQmT9YJj/L/F15Ke3zPoWOvt9LI0xuoWXMM3qatZJsXKmpa3kWuGWSBLJxLgLEGCFzZpdCIA+3CoYBZy+6TsjsWjOK84UqoQOfcjc3sBMJNqoItl7NxaoeqNd+WuhDtQ7fksZh1meBRhmvHLzRes01z7wqyHNljfZFKkEX/aw/q/FhifRuCMRkW+F7kKKk1tUHXuluz4qhSVQmqYCW9jN5OCx372GWE8qCdQgrjigK1xbJJwWkCgYEA3KwoMSyAeCjNWYirM9HTeP6JLeSptkv+nAS9UUTv9KWwOII55uXqmF3WyQUGwSumOFNiuRNwkzjkzLnNgczUImHxSvgn01j0NzL13cfpMn9a6XXWlYmljxqAakQkH5HqI1YjK+mMyx+0bom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yr+1aARgKdXn2LwWgC93aKEy/lE4aeJOTpwjjTSOSbPaK1BypMwQOGkkRh10P/+1h0MKITY0ULjaGRUwxX2E5jyaT6JHWibFwIDAQAB";
	// 这是沙箱接口路径,正式路径为https://openapi.alipay.com/gateway.do
	private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
	private final String FORMAT = "JSON";
	// 签名方式
	private final String SIGN_TYPE = "RSA2";
	// 支付宝异步通知路径,付款完毕后会异步调用本项目的方法,必须为公网地址
	private final String NOTIFY_URL = "http://公网地址/notifyUrl";
	// 支付宝同步通知路径,也就是当付款完毕后跳转本项目的页面,可以不是公网地址
	private final String RETURN_URL = "http://localhost:8080/Greenleaf/order/orderSuccess";
	
	
	/**
	 * @param out_trade_no 订单号
	 * @param total_amount 订单金额
	 * @param subject ：商品名称
	 * @param body：商品描述
	 * @param httpResponse
	 * @return
	 *///						订单号				订单金额				商品名称			商品描述
	public String alipay(String out_trade_no,String total_amount,String subject,String body) {
		// 实例化客户端,填入所需参数
		AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET,
				ALIPAY_PUBLIC_KEY, SIGN_TYPE);
		AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
		// 在公共参数中设置回跳和通知地址
		request.setReturnUrl(RETURN_URL);
		request.setNotifyUrl(NOTIFY_URL);
		//System.out.println(out_trade_no);
		request.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount
				+ "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\","
				+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
		String form = "";
		try {
			form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		
		return form;
	}
     public static void main(String[] args) {
		System.out.println(new PayUtil().alipay("202112170003","1000", "你好","aaa"));
	}
}
